package com.chinanet.handler;

import com.chinanet.entry.Account;
import com.chinanet.entry.manager.Privacy;
import com.chinanet.entry.manager.Property;
import com.chinanet.entry.manager.User;
import com.chinanet.repository.account.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;

@RepositoryEventHandler
public class AccountEventHandler {

    @Autowired
    private AccountRepository accountRepository;

    // 变相的进行关联保持
    @HandleAfterCreate
    public void handleAfterSave(Account account) {
        User user =new User();
        user.setAccount(account);
        user.setUsername(account.getUsername());
        Property property=new Property();
        user.setProperty(property);
        Privacy privacy=new Privacy();
        privacy.setName(user.getUsername());
        privacy.setDescription("还未填写基本信息");
        user.setPrivacy(privacy);
        account.setUser(user);
        accountRepository.save(account);
    }
}
